package com.guaoran.source.mybatis.v2.mapper;

import com.guaoran.source.mybatis.v2.enums.MethodTypeEnum;
import com.guaoran.source.mybatis.v2.session.SqlSession;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.List;

/**
 * @author : 孤傲然
 * @Description :
 * @date :2018/5/26 11:52
 */
public class MapperProxy<T> implements InvocationHandler {
    private final SqlSession sqlSession;
    public MapperProxy(SqlSession sqlSession) {
        this.sqlSession = sqlSession;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        MapperRegistry.KnownMappersInfo knownMappersInfo = MapperRegistry.mappersInfoMap.get(method.getDeclaringClass().getName() + "." + method.getName());
        Object o = args==null?null:args[0];
        if(knownMappersInfo != null){
            if(knownMappersInfo.getMethodType()== MethodTypeEnum.SELECT){
                if(knownMappersInfo.isReturnMany()){
                    return sqlSession.selectList(knownMappersInfo,o);
                }
                return sqlSession.selectOne(knownMappersInfo,o);
            }else{
                throw new Exception("暂不支持该类型操作。");
            }
        }
        return null;
    }
}
